﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UnityEngine.UI
{
    /// <summary>
    /// 视图组
    /// </summary>
    public class VisibleGroup : MonoBehaviour
    {
        [SerializeField]
        GameObject []gameObjects;

        [SerializeField]
        int defaultIndex = 0;

        public int Index
        {
            set { defaultIndex = value; }
            get { return defaultIndex; }
        }

        // Start is called before the first frame update
        void Start()
        {
            Show(defaultIndex);
        }

        bool Invalid(int index)
        {
            return index < 0 || index >= gameObjects.Length;
        }

        bool Invalid(string n)
        {
            return string.IsNullOrEmpty(n);
        }

        public void Show(int index)
        {
            if (Invalid(index))
                return;

            int i = 0;
            foreach (var go in gameObjects)
            {
                bool value = (i == index);
                go.SetActive(value);
                if (value)
                    Index = i;
                i++;
            }
        }

        public void Show(string n)
        {
            if (Invalid(n))
                return;

            int i = 0;
            foreach (var go in gameObjects)
            {
                string name = go.name;
                bool value = (name == n);
                go.SetActive(value);
                if (value)
                    Index = i;
                i++;
            }
        }
    }
}